//
//  LXFPlaceholderTextView.m
//

#import "LXFPlaceholderTextView.h"


@implementation LXFPlaceholderTextView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        //监听文本的输入
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //监听文本的输入
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}


- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = [placeholder copy];
    [self setNeedsDisplay];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor{
    _placeholderColor = placeholderColor;
    [self setNeedsDisplay];
}
-(void)setFont:(UIFont *)font{
    [super setFont:font];
    [self setNeedsDisplay];
}
-(void)setText:(NSString *)text{
    [super setText:text];
    [self setNeedsDisplay];
}
-(void)setAttributedText:(NSAttributedString *)attributedText{
    [super setAttributedText:attributedText];
    [self setNeedsDisplay];
}
- (void)textDidChange{
    //重绘
    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
    //如果文本框有值就返回
    if (self.hasText) return;
    CGFloat X = 5;
    CGFloat Y = 8;
    CGFloat W = rect.size.width - 2 * X;
    CGFloat H = rect.size.width - 2 * Y;
    NSMutableDictionary *attrt = [NSMutableDictionary dictionary];
    attrt[NSForegroundColorAttributeName] = self.placeholderColor?self.placeholderColor:[UIColor grayColor];
    attrt[NSFontAttributeName] = self.font?self.font:[UIFont systemFontOfSize:17.0];
    [self.placeholder drawInRect:CGRectMake(X, Y,W,H) withAttributes:attrt];
}

//移除通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
